class HomeController < ApplicationController
  def login
    @params = params.permit(:username, :password)
    if @params['username'].empty?
      @msg = ApplicationHelper.codes(40001)
      api_error({ :code => 50001, :status => 400, :msg => @msg})
    end
  end

  def initialize
    @user_service = HomeService.new
  end

  def test_user
    @data = @user_service.user_list
    api_success(data: @data)
  end

  def test1
    ["gitee_code_write1", "gitee_code_read1"]
    # return %w(gitee_code_write gitee_code_read)
  end

  def test2
    {
          "repo_path1" => ["gitee_code_write2", "gitee_code_read2"],
          "repo_path2" => ["gitee_code_manage3","gitee_code_write3", "gitee_code_read3"]
    }
  end

  def study_rugged
    # [User.new, User.new].map do |item|
    #   item.show_name
    # end

    user = User.find_by(id: 1)

    puts user
    # @user_valid = !!(user && ( user.name rescue nil))
    @user_valid ||= !!(user.present? && user&.name.present?)

    api_success(data: @user_valid)

    # data = [{:name => "reggie"}].map { |item| puts item[:name]}
    # data = [{:name => "reggie"}].map(&:name)
    # api_success(data: data)

    # [User.new, User.new].map(&:show_name)

    # path = "repo_path1"
    # data = test1 | test2.fetch(path, [])
    # data2 = test1.map
    # api_success(data: data2)

    # user = User.find_by(id: 1)
    # puts user.name
    # puts user.rename

    # path = "/data/ruby/workspace/rails_api_demo"
    # # 获取指定路径下的仓库对象
    # repo = Rugged::Repository.discover(path)
    # # 获取最新的引用，这个等于分支？
    # ref = repo.head
    # # puts ref.name
    # tree = ref.target.tree
    # root = ''
    #
    # tree.walk(:preorder).map
    #
    # files = tree.walk(:preorder).map do |root, entry|
    #   {
    #       oid: entry[:oid],
    #       path: root + entry[:name],
    #       name: entry[:name],
    #       type: entry[:type]
    #   } if %w(tree blob).include?(entry[:type].to_s)
    # end.compact
    #
    # api_success(data: files)

    # data = Array.new

    # tree.each do |item|
    #   data.push({ oid: item[:oid], name: item[:name], filemode: item[:filemode], type: item[:type]})
    # end
    #
    # api_success(data: data)
  end

  def format_file_tree(files)
    index = 0

  end

  private
end
